package com.pearadmin.pro.modules.stu.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.stu.domain.StuOrder;
import com.pearadmin.pro.modules.stu.domain.StuOrderCourse;
import com.pearadmin.pro.modules.stu.domain.StuOrderRefund;
import com.pearadmin.pro.modules.stu.param.StuOrderRefundRequest;
import com.pearadmin.pro.modules.stu.repository.StuOrderCourseRepository;
import com.pearadmin.pro.modules.stu.repository.StuOrderRefundRepository;
import com.pearadmin.pro.modules.stu.repository.StuOrderRepository;
import com.pearadmin.pro.modules.stu.service.StuOrderRefundService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


/**
 * 订单退款Service业务层处理
 *
 * @author chenJian
 * @date 2021-09-09
 */
@Service
public class StuOrderRefundServiceImpl extends ServiceImpl<StuOrderRefundRepository, StuOrderRefund> implements StuOrderRefundService {
    @Resource
    private StuOrderRefundRepository stuOrderRefundRepository;
    @Autowired
    private StuOrderRefundService stuOrderRefundService;
    @Autowired
    private StuOrderRepository stuOrderRepository;
    @Autowired
    private StuOrderCourseRepository stuOrderCourseRepository;

    @Override
    public boolean addRefundOrder(StuOrderRefund stuOrderRefund) {
        stuOrderRefundService.saveOrUpdate(stuOrderRefund);
        if (StringUtils.isNotBlank(stuOrderRefund.getOrderCourseId())) {
            StuOrder order = stuOrderRepository.selectById(stuOrderRefund.getOrderId());
            order.setPayStatus(4L);
            order.setUpdateTime(LocalDateTime.now());
            stuOrderRepository.updateById(order);
            StuOrderCourse stuOrderCourse = stuOrderCourseRepository.selectById(stuOrderRefund.getOrderCourseId());
            stuOrderCourse.setCurStatus(4l);
            stuOrderCourse.setUpdateTime(LocalDateTime.now());
            stuOrderCourseRepository.updateById(stuOrderCourse);
        }
        return true;
    }

    /**
     * 查询订单退款
     *
     * @param request
     * @return 订单退款 分页集合
     */
    @Override
    public PageResponse<StuOrderRefund> page(StuOrderRefundRequest request) {
        LambdaQueryWrapper<StuOrderRefund> query = new LambdaQueryWrapper<StuOrderRefund>();
        return Pageable.of(request, (() -> stuOrderRefundRepository.selectList(query)));
    }

    /**
     * 查询订单退款
     *
     * @param id 订单退款ID
     * @return 订单退款
     */
    @Override
    public StuOrderRefund selectStuOrderRefundById(Long id) {
        return stuOrderRefundRepository.selectById(id);
    }

}
